package com.zuocheng.librarycar;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.alibaba.fastjson.JSON;

import org.jetbrains.annotations.NotNull;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class LoginActivity extends Activity {

    public EditText phone;
    public TextView passWord;

    public static void start(Context context) {
        Intent intent = new Intent(context,LoginActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initTitle();
        setAndroidNativeLightStatusBar(this,true);

        phone = findViewById(R.id.account_input);
        passWord = findViewById(R.id.password_input);



        TextView loginBtn = (TextView)findViewById(R.id.tv_login);
        loginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                System.out.println("-------");

                String username = phone.getText().toString();
                String password = passWord.getText().toString();
                if (username.length() <= 0 || password.length() <= 0){
                    Toast.makeText(getApplicationContext(),"用户名和密码不能为空",Toast.LENGTH_SHORT).show();
                    return;
                }

                OkHttpUtil.builder().url("https://maishang.yuecunpaimai.com/prod-api/api/app/login")
//                        .addParam("username", phone.getText().toString())
//                        .addParam("password", passWord.getText().toString())
                        .addParam("username", username)
                        .addParam("password", password)
                        .post(true).async(new OkHttpUtil.ICallBack() {
                    @Override
                    public void onSuccessful(Call call, String data) {
                        System.out.println("======="+data);
                        com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(data);
                        String token = jsonObject.getString("token");
                        System.out.println("======="+token);
                        String Authorization = "Bearer  " + token;

                        //存本地
                        SharedPreferences sp = getSharedPreferences("yuecun", Context.MODE_PRIVATE);
                        SharedPreferences.Editor editor = sp.edit();
                        editor.putString("Authorization",Authorization);
                        editor.commit();

                        finish();
                    }

                    @Override
                    public void onFailure(Call call, String errorMsg) {
                        System.out.println("======="+errorMsg +call);

                    }
                });

            }
        });

        LinearLayout y = (LinearLayout)findViewById(R.id.back_aaa);
        y.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                System.out.println("-------");
                InputMethodManager imm = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm != null) {
                    imm.hideSoftInputFromWindow(view.getWindowToken(),0);
                }
            }
        });
    }


    public void sendRequest(){

        String url = "http://49.234.15.183:5351/api/app/login";
        MediaType type = MediaType.parse("application/json;charset=utf-8");

        JSONObject json = new JSONObject();
        try {
            json.put("username","zhangsan");
            json.put("password","zhangsan123");
        }catch (JSONException e){
            e.printStackTrace();
        }




        OkHttpClient client = new OkHttpClient();

        RequestBody formBody = RequestBody.create(type,String.valueOf(json));

        Request request = new Request.Builder()
                .url(url)
                .post(formBody)
                .build();


        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                System.out.println("=======+++++" +e);

            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                String data = response.body().string();
                System.out.println("=======" + data);

            }
        });
    }



    public void initTitle() {
        OverAllTitleBar rlTitle = findViewById(R.id.rl_title);
        rlTitle.tvTitle.setText("登录");
//        rlTitle.ivBack.setVisibility(View.GONE);
//        rlTitle.ivBack.setOnClickListener(v -> {
//            finish();
//        });
    }
    //设置状态栏字体颜色
    private static void setAndroidNativeLightStatusBar(Activity activity, boolean dark) {
        View decor = activity.getWindow().getDecorView();
        if (dark) {
            decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        } else {
            decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
        }
    }
    public void clickLogin(View view) {
        System.out.println("-------");




        OkHttpUtil.builder().url("https://t.fc1111.com/api/user/login")
                .addParam("client", "ios")
                .addParam("type", "1")
                .addParam("account", "555")
                .addParam("password", "123456")
                .addParam("systems", "1234")
                .post(false).async(new OkHttpUtil.ICallBack() {
            @Override
            public void onSuccessful(Call call, String data) {
                System.out.println("======="+data);
            }

            @Override
            public void onFailure(Call call, String errorMsg) {

            }
        });
    }

}
